import sys, settings, msgs, time, getopt, getpass, hashlib, hmac, socket, time
import fcntl
import struct

def usage():
	print "whatever"

try:
    	opts, args = getopt.getopt(sys.argv[1:], "h:u:s:a:", ["help", "user=", "server=", "authserver="])
except getopt.GetoptError, err:
    	usage()
    	sys.exit(2)

opCode = user = password = key = server = admin = apass = authserver = None
for o, a in opts:
       	if o in ("-h", "--help"):
     		usage()
      		sys.exit(0)
	elif o in ("-u", "--user"):
		user = a
	elif o in ("-s", "--server"):
		server = a
	elif o in ("-a", "--authserver"):
		authserver = a
        else:
       	    assert False, "unhandled option"

password = getpass.getpass("Password:")

uh = hashlib.new('sha1')
uh.update("%s:%s" % (user, password))
userHash = uh.hexdigest()

uhh = hashlib.new('sha1')
uhh.update("%s" % (userHash))
hashedUserHash = uhh.hexdigest()

data = "kinit\r\n%s\r\n" % (hashedUserHash)

conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect((server, 5023))

conn.send(data)

hashRecv = conn.recv(1024)

usend = hmac.new(userHash)
usend.update(hashRecv)
data = usend.hexdigest()


command = "ls /root"
authReq = "%s\r\n%s\r\n%s" % ("auth", data, command)
conn1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn1.connect((authserver, 5024))

conn1.send(authReq)
result = conn1.recv(7)
print result

conn.close()
conn1.close()
